package github.kawaiior.yuanchuxiuzhen;

import github.kawaiior.yuanchuxiuzhen.client.gui.market.GongFaCreateGUI;
import github.kawaiior.yuanchuxiuzhen.client.gui.market.ManaStoneConverterGUI;
import github.kawaiior.yuanchuxiuzhen.client.gui.market.MaterialConverterGUI;
import github.kawaiior.yuanchuxiuzhen.client.gui.misc.PelletFurnaceGUI;
import github.kawaiior.yuanchuxiuzhen.client.gui.misc.WeaponFurnaceGUI;
import github.kawaiior.yuanchuxiuzhen.core.init.StructureInit;
import github.kawaiior.yuanchuxiuzhen.core.init.*;
import github.kawaiior.yuanchuxiuzhen.core.init.event.EntitySpawnEvent;
import github.kawaiior.yuanchuxiuzhen.core.init.event.OreGenerationEvent;
import github.kawaiior.yuanchuxiuzhen.network.NetworkRegistryHandler;
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Locale;

// The value here should match an entry in the META-INF/mods.toml file
@Mod(Yuanchuxiuzhen.MODID)
public class Yuanchuxiuzhen {
    // Directly reference a log4j logger.
    private static final Logger LOGGER = LogManager.getLogger();
    public static final String MODID = "yuanchuxiuzhen";

    private static final boolean DEBUG = true;

    public Yuanchuxiuzhen() {
        // Register ourselves for server and other game events we are interested in
//        MinecraftForge.EVENT_BUS.register(this);

        IEventBus modbus = FMLJavaModLoadingContext.get().getModEventBus();

        ItemInit.ITEMS.register(modbus);
        BlockInit.BLOCKS.register(modbus);
        EnchantmentInit.ENCHANTMENTS.register(modbus);

        StructureInit.STRUCTURES.register(modbus);
        BiomeInit.BIOMES.register(modbus);

        EffectInit.POTIONS.register(modbus);
        PotionInit.POTION_TYPES.register(modbus);

        EntityInit.ENTITIES.register(modbus);
        // 这个tileentity
        TileEntityInit.TILE_ENTITY_TYPES.register(modbus);//bug

        ContainerInit.CONTAINER_TYPE.register(modbus);
        // 生物生成事件
        MinecraftForge.EVENT_BUS.addListener(EventPriority.HIGH, EntitySpawnEvent::BiomeLoading);
        MinecraftForge.EVENT_BUS.addListener(EventPriority.HIGH, OreGenerationEvent::generateOres);

        // init
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::init);

        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::onCommonSetUp);
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::onClientSetup);
    }

    @SubscribeEvent
    public void init(FMLCommonSetupEvent event){
        DimensionInit.init();
        event.enqueueWork(()->{
            StructureInit.setupStructures();
        });
    }


    public void onCommonSetUp(FMLCommonSetupEvent event){
        NetworkRegistryHandler.registerMessage();
    }

    public void onClientSetup(FMLClientSetupEvent event){
        event.enqueueWork(()->{
            ScreenManager.registerFactory(ContainerInit.MATERIAL_CONVERTER_GUI_CONTAINER.get(), MaterialConverterGUI::new);
            ScreenManager.registerFactory(ContainerInit.GONG_FA_CREATE_CONTAINER.get(), GongFaCreateGUI::new);
            ScreenManager.registerFactory(ContainerInit.MANA_STONE_CONVERTER_CONTAINER.get(), ManaStoneConverterGUI::new);

            ScreenManager.registerFactory(ContainerInit.PELLET_FURNACE_CONTAINER.get(), PelletFurnaceGUI::new);
            ScreenManager.registerFactory(ContainerInit.WEAPON_FURNACE_CONTAINER.get(), WeaponFurnaceGUI::new);
        });
    }

    public static void debug(String s){
        if (DEBUG) {
            LOGGER.debug(s);
        }
    }

    public static void debug(Object o){
        if (DEBUG){
            LOGGER.debug(o.toString());
        }
    }

    public static void info(String s){
        LOGGER.info(s);
    }

    public static void info(Object o){
        LOGGER.info(o);
    }

    public static void warning(String s){
        LOGGER.warn(s);
    }

    public static void waring(Object o){
        LOGGER.warn(o);
    }

    public static ResourceLocation prefix(String name) {
        return new ResourceLocation(MODID, name.toLowerCase(Locale.ROOT));
    }

}
